package com.lihao.sam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.sam.dao.ArticleExWarehouseRegistrationDao;
import com.lihao.sam.dao.InventoryOfExistingArticlesDao;
import com.lihao.sam.dto.ArticlesDto;
import com.lihao.sam.dto.StatisticsDto;
import com.lihao.sam.entity.xiaobei.ArticleExWarehouseRegistration;
import com.lihao.sam.entity.xiaobei.InventoryOfExistingArticles;
import com.lihao.sam.service.intf.ArticleExWarehouseRegistrationService;
import com.lihao.sam.service.intf.InventoryOfExistingArticlesService;
import com.lihao.sam.vo.PageVo;
import com.lihao.sam.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yifu
 * @date2022/11/22 19:51
 */
@Service
public class InventoryOfExistingArticlesServiceImpl implements InventoryOfExistingArticlesService {

    @Resource
    private InventoryOfExistingArticlesDao dao;
    @Override
    public R add(InventoryOfExistingArticles inventoryOfExistingArticles) {
        //1.校验参数
        if(inventoryOfExistingArticles!=null){
            //2.操作数据库
            if(dao.add(inventoryOfExistingArticles)>0){
                return R.ok();
            }
        }
        return R.fail();
        }

    @Override
    public R update(InventoryOfExistingArticles inventoryOfExistingArticles) {
        //1.校验参数
        if(inventoryOfExistingArticles!=null){
            //2.操作数据库
            if(dao.update(inventoryOfExistingArticles)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delete(int id) {
        //1.校验参数
        if(id>0){
            //2.操作数据库
            if(dao.delete(id)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delBatch(int[] array) {
        if (dao.delBatch(array) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public PageVo queryPage(String goodsid,String goodsname, String specification, int page, int limit) {
        //1.开启分页，设置对应的页码
        PageHelper.startPage(page,limit);
        //2.开始查询
        PageInfo<InventoryOfExistingArticles> pageInfo=new PageInfo<>(dao.selectByWhere(goodsid, goodsname,specification));
        //3.返回对应的结果
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R selecthand() {
        //1.查询
        List<ArticlesDto> list = dao.selecthand();
        //2.组装结果数据
        Map<String, ArrayList> map = new HashMap<>();
        map.put("goodsname",new ArrayList<>());
        map.put("money",new ArrayList<>());
        map.put("num",new ArrayList<>());
        for (ArticlesDto au : list) {
            map.get("goodsname").add(au.getGoodsname());
            map.get("money").add(au.getMoney());
            map.get("num").add(au.getNum());
        }
        return R.ok(map);
    }
    }



